package com.wskj.insuranceweb.reporter.controller;

import com.wskj.insuranceweb.reporter.pojo.*;
import com.wskj.insuranceweb.reporter.service.AcceptService;
import com.wskj.insuranceweb.reporter.service.ClientService;
import com.wskj.insuranceweb.reporter.service.FindingService;
import com.wskj.insuranceweb.reporter.service.ReporterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/accept")
public class AcceptController {

    @Autowired
    private AcceptService service;
    @Autowired
    private FindingService findingService;
    @Autowired
    private ReporterService reporterService;
    @Autowired
    private ClientService clientService;



    @RequestMapping("/list1")
    public String list(){
        return "accept/list";
    }
    @RequestMapping("/list")
    @ResponseBody
    public List<Accept> showList(String report,String customernumber){

        List<Accept> list=  service.showList(report,customernumber);

        return list;
    }

    @RequestMapping("/lookAccept")
    public String lookAccept(String cust , Model model){

        Accept accept = service.getAccept(cust);
        Findings s = findingService.getFinding(cust);
        Reporter reporter = reporterService.getReporter(cust);
        Claim claim = service.findClaim(cust);
        if(claim==null){
            claim = service.getClaim(reporter.getClaimstatus(),s.getFindings(),s.getCustomernumber());
        }
        model.addAttribute("claim",claim);
        model.addAttribute("all",reporter);
        model.addAttribute("accept",accept);
        model.addAttribute("finding",s);

        return "accept/look";
    }

    @RequestMapping("/joinpool")
    public String joinpool(String id, Model model){
        System.out.println(id);
            Accept accept = service.findAcceptById(id);
            model.addAttribute("accept",accept);
            return "accept/update";
    }

    @RequestMapping("/joinpool1")
    public String joinpool(String id, String name){
      service.updateById(id,name);
        return "accept/update";
    }

    @RequestMapping("/waitpool")
    public String waitpool(){
        return "accept/waitpool";
    }
    @RequestMapping("/waitList")
    @ResponseBody
    public List<Accept> waitList(){
        List<Accept> list = service.findAccept();
        return list;
    }

    @RequestMapping("/updateInvestigation")
    @ResponseBody
    public String updateInvestigation(String id){
        service.updateInvestigation(id);
        return "";
    }

    @RequestMapping("/addFindings")
    public String addFindings(String cust,Model model){
        model.addAttribute("cust",cust);
       return "accept/addFindings";
    }

    @RequestMapping("/addFindings1")
    public String addFindings(Findings findings){
        System.out.println(findings);
        findingService.addFindings(findings);
        service.updateByCust(findings.getCustomernumber());
        return "accept/addFindings";
    }

    @RequestMapping("/updateCause")
    @ResponseBody
    public String updateCause(String cust){
        service.updateCauseByCust(cust);
        return "";
    }
    @RequestMapping("/editC")
    @ResponseBody
    public String editInvestigation(String cust){
        service.updateCByCust(cust);
        return "";
    }
    @RequestMapping("/suspend")
    public String suspend(){
        return "accept/suspend";
    }

    @RequestMapping("/suspendList")
    @ResponseBody
    public List<Accept> suspendList(){
        List<Accept> list = service.findAcceptBySuspend();
        return list;
    }

    @RequestMapping("/joinsuspend")
    @ResponseBody
    public String joinsuspend(String cust){
        service.editpond(cust);
        return "";
    }

    @RequestMapping("/updateloding")
    @ResponseBody
    public String returnPool(String cust){
        service.returnPool(cust);
        return "";
    }

    @RequestMapping("/deleteReporter")
    public String deleteReporter(String cust,String rejection){
        service.deleteAccept(cust);
        reporterService.updateReporterByCust(cust,rejection);
        return "reporter/update";
    }

    @RequestMapping("/findInfo")
    public String info(String reporter , Model model){
        Backuparea b = service.findBackuparea(reporter);
        Reporter re = reporterService.findReporterByReporter(reporter);
        List<Modifycause> m = service.findModifycause(reporter);
        model.addAttribute("modify",m);
        model.addAttribute("back",b);
        model.addAttribute("re",re);
        return "accept/info";
    }

    @RequestMapping("/updateInfo")
    public String updateInfo(Backuparea b, Modifycause modifycause, HttpSession session){

        modifycause.setReport(b.getReport());
        Employee e = (Employee) session.getAttribute("employee");
        String a = b.getAccidentdate().substring(0,10);
        String c = b.getReportdate().substring(0,10);
        b.setAccidentdate(a);
        b.setReportdate(c);
        modifycause.setEmpolyeename(e.getName());
        System.out.println(b);
        System.out.println(modifycause);

        reporterService.update(b);
        service.update(b);
        service.addModifycause(modifycause);


        return "reporter/update";
    }

    @RequestMapping("/conclusion")
    public String conclu(Conclusion conclusion){
        if (conclusion.getConclusion().equals("给付")||conclusion.getConclusion().equals("部分给付")){
            service.editconcl(conclusion.getReport());
        }else{
            reporterService.editReporter(conclusion.getReport(),conclusion.getConclusion());
            service.editclaim(conclusion.getReport());
        }
        service.addConclusion(conclusion);
        return "reporter/update";
    }
    @RequestMapping("/examineList1")
    public String list1(){
        return "examine/list";
    }
    @RequestMapping("/examineList")
    @ResponseBody
    public List<Accept> list(String report,String customernumber){

        List<Accept> accepts = service.AcceptList(report,customernumber);
        return accepts;
    }

    @RequestMapping("/lookExamine")
    public String lookExamine(String cust , Model model){

        Accept accept = service.getAccept(cust);
        Findings s = findingService.getFinding(cust);
        Reporter reporter = reporterService.getReporter(cust);
        Claim claim = service.findClaim(cust);
        Conclusion conclusion = service.getConclusion(accept.getReport());

        System.out.println(conclusion);
        model.addAttribute("claim",claim);
        model.addAttribute("all",reporter);
        model.addAttribute("accept",accept);
        model.addAttribute("finding",s);
        model.addAttribute("exa",conclusion);

        return "examine/look";
    }

    @RequestMapping("/ifexamine")
    public String ifExamine(Examine examine){
        if(examine.getExaminesion().equals("通过")){

            service.updateClaim(examine.getReport());
        }else{

        }
        service.addExamine(examine);
        return "reporter/update";
    }



}
